home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Science / µSim 1.0b5 folder / source / Preferences.c < prev    next >
Encoding:
Text File  |  1994-09-01  |  6.4 KB  |  214 lines  |  [TEXT/MMCC]

  1. /*
  2. Copyright © 1993,1994 by Fabrizio Oddone
  3. ••• ••• ••• ••• ••• ••• ••• ••• ••• •••
  4. This source code is distributed as freeware: you can copy, exchange, modify this
  5. code as you wish. You may include this code in any kind of application: freeware,
  6. shareware, or commercial, provided that full credits are given.
  7. You may not sell or distribute this code for profit.
  8. */
  9.  
  10. //#pragma load "MacDump"
  11.  
  12. #include    "UtilsSys7.h"
  13. #include    "Disasm.h"
  14. #include    "DoEditDialog.h"
  15. #include    "Dump.h"
  16. #include    "Globals.h"
  17. #include    "Preferences.h"
  18. #include    "Main.h"
  19. #include    "Registers.h"
  20. #include    "Conversions.h"
  21.  
  22. #if defined(FabSystem7orlater)
  23.  
  24. static pascal Boolean PrefsFilter(DialogPtr theD, EventRecord *thEv, short *iHit);
  25. static void SetTopLeft(Point *thePt, WindowPtr w);
  26.  
  27. #pragma segment Rare
  28.  
  29. enum {
  30. kItemRememberWindPos = 3,
  31. kItemContinuousDumpScroll,
  32. kItemInfLoopsDetect,
  33. kItemInitialPCVal,
  34. kItemInitialSPVal,
  35. kItemStackSize
  36. };
  37.  
  38. void Preferences(void)
  39. {
  40. enum {
  41. kDLOG_Prefs = 262
  42. };
  43.  
  44. Str255    InitPCStr, InitSPStr, StackSizeStr;
  45. long    dummy;
  46.  
  47. dialogItems    things[] = {{ ok, 0, 1L },
  48.                         { cancel, 0, 0L },
  49.                         { kItemRememberWindPos, 0, 0L },
  50.                         { kItemContinuousDumpScroll, 0, 0L },
  51.                         { kItemInfLoopsDetect, 0, 0L },
  52.                         { kItemInitialPCVal, 0, 0L },
  53.                         { kItemInitialSPVal, 0, 0L },
  54.                         { kItemStackSize, 0, 0L },
  55.                         { 0, 0, 0L}
  56.                         };
  57.  
  58. things[kItemRememberWindPos-1].refCon = gPrefs.remembWind;
  59. things[kItemContinuousDumpScroll-1].refCon = gPrefs.NeXTScroll;
  60. things[kItemInfLoopsDetect-1].refCon = gPrefs.infLoopsDetect;
  61. things[kItemInitialPCVal-1].refCon = (long)&InitPCStr;
  62. things[kItemInitialSPVal-1].refCon = (long)&InitSPStr;
  63. things[kItemStackSize-1].refCon = (long)&StackSizeStr;
  64.  
  65. ShortToHexString(gPrefs.DefPCValue, InitPCStr);
  66. ShortToHexString(gPrefs.DefSPValue, InitSPStr);
  67. MyNumToString(gPrefs.DefStkSize, StackSizeStr);
  68.  
  69. if (HandleDialog(PrefsFilter, things, nil, nil, kDLOG_Prefs) == ok) {
  70.     gPrefs.remembWind = things[kItemRememberWindPos-1].refCon;
  71.     gPrefs.NeXTScroll = things[kItemContinuousDumpScroll-1].refCon;
  72.     gPrefs.infLoopsDetect = things[kItemInfLoopsDetect-1].refCon;
  73.     HexStringToShort(InitPCStr, (short *)&gPrefs.DefPCValue);
  74.     HexStringToShort(InitSPStr, (short *)&gPrefs.DefSPValue);
  75.     StringToNum(StackSizeStr, &dummy);
  76.     gPrefs.DefStkSize = dummy;
  77.     }
  78. }
  79.  
  80. /* PrefsFilter: the filterProc routine for the Prefs dialog */
  81.  
  82. static pascal Boolean PrefsFilter(DialogPtr theD, EventRecord *thEv, short *iHit)
  83. {
  84. GrafPtr    savePort;
  85. unsigned char    keypressed;
  86. register Boolean    retVal;
  87.  
  88. switch(thEv->what) {
  89.     case keyDown    :
  90.     case autoKey    :
  91.         keypressed = CHARFROMMESSAGE(thEv->message);
  92.         if ((keypressed >= 'a') && (keypressed <= 'z')) {
  93.             keypressed -= 'a' - 'A';    /* cambiare! */
  94.             CHARFROMMESSAGE(thEv->message) = keypressed;
  95.             }
  96.         if ((keypressed >= 32) && (keypressed != 0x7F) && ((thEv->modifiers & cmdKey) == 0)) {
  97.             *iHit = ((DialogPeek)theD)->editField + 1;
  98.             switch( *iHit ) {
  99.                 case kItemInitialPCVal:
  100.                 case kItemInitialSPVal:
  101.                     return( Munger((Handle)GetString(kSTR_HEXALLOWED), 1L, &keypressed,
  102.                                     1L, 0L, 0L) < 0L );
  103.                     break;
  104.                 case kItemStackSize:
  105.                     return( Munger((Handle)GetString(kSTR_DECALLOWED), 1L, &keypressed,
  106.                                     1L, 0L, 0L) < 0L );
  107.                     break;
  108.                 }
  109.             }
  110.         break;
  111.     case updateEvt:
  112.         if (theD != (DialogPtr)thEv->message) {
  113.             DoUpdate(thEv);
  114.             *iHit = kfakeUpdateItem;
  115.             return true;
  116.             }
  117.         break;
  118.     case activateEvt:
  119.         if (theD != (DialogPtr)thEv->message) {
  120.             DoActivate(thEv);
  121.             *iHit = kfakeUpdateItem;
  122.             return true;
  123.             }
  124.         break;
  125.     }
  126. GetPort(&savePort);
  127. SetPort(theD);
  128. retVal = StdFilterProc(theD, thEv, iHit);
  129. SetPort(savePort);
  130. return retVal;
  131. }
  132.  
  133. #pragma segment CleanUp
  134.  
  135. /* SavePreferencesFile: in the end we save our gPrefs */
  136.  
  137. void SavePreferencesFile(void)
  138. {
  139. ParamBlockRec    myPB;
  140. EventRecord    dummyEv;
  141. FSSpec    myFSS;
  142. register Handle    myStrHand;
  143. short    prefsFRefNum;
  144. Boolean    targetFolder, isAnAlias;
  145. register OSErr    err;
  146. register SignedByte    oldState;
  147.  
  148. oldState = WantThisHandleSafe(myStrHand = (Handle)GetString(kSTR_PREFSFILENAME));
  149. if ((err = FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
  150.             &myFSS.vRefNum, &myFSS.parID)) == noErr) {
  151.     err = FSMakeFSSpec(myFSS.vRefNum, myFSS.parID, (ConstStr255Param)*myStrHand, &myFSS);
  152.     if ((err == noErr) || (err == fnfErr)) {
  153.         if (err == fnfErr)
  154. #ifndef __SCRIPT__
  155. #define    smSystemScript    -1
  156. #endif
  157.             err = FSpCreate(&myFSS, '????', kPreferencesFolderType, smSystemScript);
  158.         if (err == noErr)
  159.             if ((err = ResolveAliasFile(&myFSS, true, &targetFolder, &isAnAlias)) == noErr)
  160.                 if (targetFolder)
  161.                     err = paramErr;
  162.                 else
  163.                     if ((err = FSpOpenDF(&myFSS, fsWrPerm, &prefsFRefNum)) == noErr) {
  164.                         SetTopLeft(&gPrefs.AnimTopLeft, gWPtr_Animation);
  165.                         SetTopLeft(&gPrefs.RegsTopLeft, gWPtr_Registers);
  166.                         gPrefs.RegsBase = GetCtlValue(Ctrl_Base);
  167.                         gPrefs.IOUserState = (*(WStateDataHandle)((WindowPeek)gWPtr_IO)->dataHandle)->userState;
  168.                         SetTopLeft(&gPrefs.MProgTopLeft, gWPtr_Microprogram_Ed);
  169.                         SetTopLeft(&gPrefs.DumpTopLeft, gWPtr_Dump);
  170.                         gPrefs.DumpHeight = PRCT_B(gWPtr_Dump) - PRCT_T(gWPtr_Dump);
  171.                         gPrefs.DumpScrollVal = GetCtlValue(dumpVScroll);
  172.                         SetTopLeft(&gPrefs.DisasmTopLeft, gWPtr_Disasm);
  173.                         gPrefs.DisasmHeight = PRCT_B(gWPtr_Disasm) - PRCT_T(gWPtr_Disasm);
  174.                         gPrefs.DisasmScrollVal = GetCtlValue(disasmVScroll);
  175.                         gPrefs.AnimVisible = ((WindowPeek)gWPtr_Animation)->visible;
  176.                         gPrefs.RegsVisible = ((WindowPeek)gWPtr_Registers)->visible;
  177.                         gPrefs.IOVisible = ((WindowPeek)gWPtr_IO)->visible;
  178.                         gPrefs.DumpVisible = ((WindowPeek)gWPtr_Dump)->visible;
  179.                         gPrefs.DisasmVisible = ((WindowPeek)gWPtr_Disasm)->visible;
  180.                         myPB.ioParam.ioCompletion = 0L;
  181.                         myPB.ioParam.ioRefNum = prefsFRefNum;
  182.                         myPB.ioParam.ioBuffer = (Ptr)&gPrefs;
  183.                         myPB.ioParam.ioReqCount = sizeof(struct myprefs);
  184.                         myPB.ioParam.ioPosMode = fsFromStart;
  185.                         myPB.ioParam.ioPosOffset = 0L;
  186.                         (void)PBWriteAsync(&myPB);
  187.                         while (myPB.ioParam.ioResult > 0) {
  188.                             SystemTask();
  189.                             (void)EventAvail(everyEvent, &dummyEv);
  190.                             }
  191.                         (void)FSClose(prefsFRefNum);
  192.                         if ((err = myPB.ioParam.ioResult) == noErr)
  193.                             err = AddSTRRes2Doc(&myFSS, '????', kPreferencesFolderType,
  194.                                                 kSTR_NOOPENORPRINT, smSystemScript);
  195.                         }
  196.         }
  197.     }
  198. HSetState(myStrHand, oldState);
  199. }
  200.  
  201. /* SetTopLeft: common routine which calculates the topLeft coordinate
  202. of a GrafPort’s portRect in global coordinates;
  203. it _changes_ the current port */
  204.  
  205. static void SetTopLeft(Point *thePt, GrafPtr w)
  206. {
  207. *thePt = topLeft(w->portRect);
  208. SetPort(w);
  209. LocalToGlobal(thePt);
  210. }
  211.  
  212. #endif
  213.  
  214.